<?php  
class ControllerCommonHome extends Controller {
	public function index() {
		$this->language->load('common/home');   
		 
        $this->load->model('tool/seo_url'); 
        $this->load->model('catalog/category');
        $this->load->model('tool/image');             
        $this->load->model('catalog/manufacturer');
         
		$this->document->title = $this->config->get('config_title');
        $this->document->keywords = $this->config->get('config_meta_keywords');
		$this->document->description = $this->config->get('config_meta_description');
        //$this->document->title = $this->language->get('meta_title');
        //$this->document->keywords = $this->language->get('meta_keywords');
        //$this->document->description = $this->language->get('meta_description');
		
		$this->data['search_keywords'] = $this->config->get('config_search_keywords');   
		$this->data['heading_title'] = sprintf($this->language->get('heading_title'), $this->config->get('config_name'));
		$this->data['template'] = $this->config->get('config_template');  		
		$this->data['products'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/categories'); 
		 
        $this->data['flash_url'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=common/home/flashData');    
        $this->data['search_url'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/search');
        $this->data['welcome_video'] = HTTP_SERVER . 'image/video/havhokeren_sailxtreme.flv';
        $this->data['giftcard_link'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=checkout/giftcard');
		
		$categories = array();
		$this->data['categories'] = $this->getCategories(0);       
		
        /*
		$this->load->model('setting/store');                                                     		
		if (!$this->config->get('config_store_id')) {
			$this->data['welcome'] = html_entity_decode($this->config->get('config_description_' . $this->config->get('config_language_id')), ENT_QUOTES, 'UTF-8');
		} 
        else {
			$store_info = $this->model_setting_store->getStore($this->config->get('config_store_id'));
			
			if ($store_info) {
				$this->data['welcome'] = html_entity_decode($store_info['description'], ENT_QUOTES, 'UTF-8');
			} 
            else {
				$this->data['welcome'] = '';
			}
		}
		*/

        $this->load->model('catalog/information');
        $welcome_info = $this->model_catalog_information->getInformation(3);
        $desc_info = html_entity_decode($welcome_info['description']);
        /*
        if(stripos($desc_info, '<hr />')){
            $description = explode('<hr />', $desc_info);              
            $description = strip_tags($description[0], '<a><p><span><strong>');
        }
        elseif(stripos($desc_info, '{READ_MORE}')){
            $description = explode('{READ_MORE}', $desc_info);              
            $description = strip_tags($description[0], '<a><p><span><strong>');             
        }        
        else{
            $description = (strlen($desc_info) > 396) ? (mb_substr(strip_tags($desc_info, '<a><p>'), 0, 400) . ' ...') : $desc_info;    
        }
        */
        $description = strip_tags($desc_info, '<a><p><span><strong>');

        $this->data['welcome'] = array(
            'text'  => $description,
            //'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/information&information_id=3'),
            'href'  => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=information/information&information_id=4'),
        );
        	
        $service_info = $this->model_catalog_information->getInformation(13);
        $this->data['customer_service'] = html_entity_decode($service_info['description']);
        
        /* footer */
        $manufacturers = $this->getManufacturers();         
        $this->data['manufacturers'] = $manufacturers;
        
        $categories = array();       
        $this->load->model('content/category');    
        $this->load->model('content/content');             
        $categories_data = $this->model_content_category->getCategories(0, 1, 4); 
        foreach($categories_data as $category){
            $contents = $this->model_content_content->getContentsByCategoryId($category['category_id'], 'p.sort_order, pd.name', 'ASC', 35, 0);
            
            foreach($contents as &$c_item){
                if($c_item['article_type'] == 'article'){
                    $c_item['href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=content/item/article&content_id=' . $c_item['content_id']);
                }
                else{
                    if($c_item['url_type'] == 'internal'){
                        $c_item['href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . $c_item['url_content']);
                    }
                    else{
                        $c_item['href'] = $c_item['url_content'];    
                    }
                }
            }
            
            $categories[] = array(
                'name'          => $category['name'],
                'children'      => $contents,
            );            
        }        
        $this->data['content_categories'] = $categories; 
        
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/home.tpl';
		} else {
			$this->template = 'default/template/common/home.tpl';
		}
		
		$this->language->load('common/footer');
		
        if(stristr($this->config->get('config_email'), 'havhokeren.dk') !== false){
            $email = $this->config->get('config_email');                
        }
        else{
            $email = 'webshop@havhokeren.dk';
        }
        
        $powered_by = 'Havhøkeren ApS - '; //$this->config->get('config_owner') . ' - ';
        $powered_by .= str_replace("\n", ' - ', $this->config->get('config_address')) . ' - ';
		$powered_by .= 'Tlf. ' . $this->config->get('config_telephone') . ' - ';
		$powered_by .= '<a href="mailto:' . $email . '">' . $email . '</a>';
		$powered_by .= ' - <b>CVR-nr. 34603278</b>';		
        $this->data['text_powered_by'] = $powered_by;
        //$this->data['text_powered_by'] = sprintf($this->language->get('text_powered_by'), date('Y', time()));
        
		$this->children = array(
			'common/header',  			
			'common/footer',
			//'common/column_left_home',
            'module/services',
			'module/product',
			'module/category',
			'module/banner',
		);

		$this->load->model('checkout/extension');   		
		$module_data = $this->model_checkout_extension->getExtensionsByPosition('module', 'home'); 		
		$this->data['modules'] = $module_data;    		
		foreach ($module_data as $result) {
			$this->children[] = 'module/' . $result['code'];
		}

		$manufacturers = $this->getManufacturers();
		$this->data['manufacturers'] = $manufacturers;

        if ($this->isMobileEnabled()) {
            $this->children = array(
                'common/header',
                'common/footer',
                'module/services',
                'module/product',
                'module/banner',
                'module/newsletter_home',
            );
        }

		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
	
	public function flashData(){		
        $path = DIR_IMAGE . 'banner/';  
		$file_ext = '.jpg';
        
        $output = '<?xml version="1.0" encoding="UTF-8"?>';
        $output .= '<slideshow width = "748" height = "286" startWith = "1" backgroundColor = "000000" backgroundTransparency = "0" randomSlideshow = "true" loop = "true">';
        
        $files = glob(rtrim($path, '/') . '/*'); 
        foreach ($files as $file) {
            $output .= '    <item>';
            $output .= '        <path>' . 'image/banner/' . basename($file) . '</path>';
            $output .= '        <transitionTime>3</transitionTime>';
            $output .= '        <slideShowTime>5</slideShowTime>';
            $output .= '    </item>';
        }
                          
        $output .= '</slideshow>';
        
        echo $output;
        return;
        
        // Change file location        
        $path = 'catalog/view/theme/' . $this->config->get('config_template') . '/';          
        $file_ext = '.jpg';
        
        /*
		$output = '<?xml version="1.0" encoding="iso-8859-1"?>';
		$output .= '<coxandcox showNav="false">';
		$output .= '	<img mainImage="' . $path . 'swf/large/1' . $file_ext . '" popupTitle="ESTABLISHED 1985" popupText="Cox &amp; Cox have been creating exceptional living spaces since 1985" popupImage="pop/established_2.jpg" linkURL="#" />';
		$output .= '	<img mainImage="' . $path . 'swf/large/2' . $file_ext . '" popupTitle="Classic Interior Design" popupText="Cox &amp; Cox have an extensive design portfolio of classic country properties " popupImage="pop/Living_room_6_1.jpg" linkURL="#" />';
		$output .= '	<img mainImage="' . $path . 'swf/large/3' . $file_ext . '" popupTitle="Contemporary Interior Design" popupText="Cox &amp; Cox has a wealth of experience delivering contemporary interiors" popupImage="pop/INterior_design_Butler_1.jpg" linkURL="#" />';
		$output .= '	<img mainImage="' . $path . 'swf/large/4' . $file_ext . '" popupTitle="Contemporary Interior Design" popupText="Cox &amp; Cox has a wealth of experience delivering contemporary interiors" popupImage="pop/INterior_design_Butler_1.jpg" linkURL="#" />';
		$output .= '</coxandcox>';
		*/
        
        $output = '<?xml version="1.0" encoding="UTF-8"?>';
        $output .= '<slideshow width = "748" height = "286" startWith = "1" backgroundColor = "000000" backgroundTransparency = "0" randomSlideshow = "true" loop = "true">';
        $output .= '    <item>';
        $output .= '        <path>' . $path . 'swf/img/1' . $file_ext . '</path>';
        $output .= '        <transitionTime>3</transitionTime>';
        $output .= '        <slideShowTime>5</slideShowTime>';
        $output .= '    </item>';
        $output .= '    <item>';
        $output .= '        <path>' . $path . 'swf/img/1' . $file_ext . '</path>';
        $output .= '        <transitionTime>3</transitionTime>';
        $output .= '        <slideShowTime>5</slideShowTime>';
        $output .= '    </item>';
        $output .= '    <item>';
        $output .= '        <path>' . $path . 'swf/img/1' . $file_ext . '</path>';
        $output .= '        <transitionTime>3</transitionTime>';
        $output .= '        <slideShowTime>5</slideShowTime>';
        $output .= '    </item>';
        $output .= '    <item>';
        $output .= '        <path>' . $path . 'swf/img/1' . $file_ext . '</path>';
        $output .= '        <transitionTime>3</transitionTime>';
        $output .= '        <slideShowTime>5</slideShowTime>';
        $output .= '    </item>';                       
        $output .= '</slideshow>';
        
		echo $output;
	}
    
    private function getCategories($parent_id) { 
        $data = array();
        
        $results = $this->model_catalog_category->getCategories($parent_id);           
        foreach ($results as $result) {    
            $children_data = $this->model_catalog_category->getCategories($result['category_id']);
            $children = array();
            /*
            foreach($children_data as $child){
                $link = HTTP_SERVER . 'index.php?route=product/category&amp;path=' . $result['category_id'] . '_' . $child['category_id']; 
                $children[] = array(
                    //'category_id'       => $child['category_id'],
                    'name'              => $child['name'],
                    'href'              => $this->model_tool_seo_url->rewrite($link),                      
                    //'meta_keywords'     => $child['meta_keywords'],
                    //'meta_description'  => $child['meta_description'],                    
                );    
            }
            */
            $children = array();
            /*
            $children_data = $this->model_catalog_manufacturer->getManufacturersByCategory($result['category_id']);            
            foreach($children_data as $manufacturer){
                $link = HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $manufacturer['manufacturer_id'] . '&path=' . $result['category_id'];
                
                $children[] = array(
                    'name'      =>  $manufacturer['name'],                    
                    'href'      =>  $this->model_tool_seo_url->rewrite($link),
                );    
            }
            */
            
            //$image = $result['image'] ? $result['image'] : 'no_image.jpg';
            $link = HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $result['category_id'];              
            $data[] = array(
                //'category_id'       => $result['category_id'],
                'name'              => $result['name'],
                'href'              => $this->model_tool_seo_url->rewrite($link),
                //'image'           => $this->model_tool_image->resize($image, 229, 112),
                'meta_keywords'     => $result['meta_keywords'],
                'meta_description'  => $result['meta_description'],
                'children'          => $children,
            );
        }
       
        return $data;
    }
    
    private function getManufacturers(){
        //$manufacturers_data = $this->model_catalog_manufacturer->getManufacturersPath();
        $manufacturers_data = $this->model_catalog_manufacturer->getManufacturers();
        
        $manufacturers = array();
        foreach($manufacturers_data as $manufacturer_item){
            //$path = $manufacturer_item['path'];
            
            $manufacturers[] = array(
                'name'      => $manufacturer_item['name'],
                'image'     => $this->model_tool_image->resize($manufacturer_item['image'], 94, 50),
                //'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $manufacturer_item['manufacturer_id'] . '&path=' . $path),
                'href'      => $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/manufacturer/viewManufacturer&manufacturer_id=' . $manufacturer_item['manufacturer_id']),
            );    
        }
        
        return $manufacturers;
    }
	
	public function facebook(){
		$this->data['title'] = $this->config->get('config_title');
		$this->data['template'] = $this->config->get('config_template');
		$this->data['store_url'] = HTTP_SERVER;
		
		$this->load->model('tool/seo_url');
		$this->load->model('catalog/category');
		$this->data['categories'] = array();
		$categories = $this->model_catalog_category->getCategories(0);
		foreach($categories as $category){
			$link = HTTP_SERVER . 'index.php?route=product/categories&amp;path=' . $category['category_id'];
			
			$this->data['categories'][] = array(				
				'name'              => $category['name'],
				'href'              => $this->model_tool_seo_url->rewrite($link),				
			);
		}
		
		$this->data['giftcard_link'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=checkout/giftcard');
		$this->data['newsletter_page'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=newsletter/newsletter');
		
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/fb.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/common/fb.tpl';
		} else {
			$this->template = 'default/template/common/fb.tpl';
		}
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}
}
?>